# -*- coding: utf-8 -*- 
# 开发者: 夏明德           
# 开发时间 : 2022/12/26 11:10
# 文件名称: BMI对比公式.py
import math
import os
import openpyxl


class bmi(object):

    def hight_Bmi_Man(self, age, num):
        """
        接收传递的值，通过年龄来比对体重指数并返回结果！
        """
        if age == 6:
            if num <= 13.2:
                return '中重度消瘦'
            if num < 13.4:
                return '轻度消瘦'
            if num < 16.7:
                return '正常'
            if num < 18.1:
                return '超重'
            if num >= 18.1:
                return '肥胖'
        if age == 7:
            if num <= 13.5:
                return '中重度消瘦'
            if num < 13.9:
                return '轻度消瘦'
            if num < 17.4:
                return '正常'
            if num < 19.2:
                return '超重'
            if num >= 19.2:
                return '肥胖'
        if age == 8:
            if num <= 13.6:
                return '中重度消瘦'
            if num < 14.0:
                return '轻度消瘦'
            if num < 18.1:
                return '正常'
            if num < 20.3:
                return '超重'
            if num >= 20.3:
                return '肥胖'
        if age == 9:
            if num <= 13.7:
                return '中重度消瘦'
            if num < 14.1:
                return '轻度消瘦'
            if num < 18.9:
                return '正常'
            if num < 21.4:
                return '超重'
            if num >= 21.4:
                return '肥胖'
        if age == 10:
            if num <= 13.9:
                return '中重度消瘦'
            if num < 14.4:
                return '轻度消瘦'
            if num < 19.6:
                return '正常'
            if num < 22.5:
                return '超重'
            if num >= 22.5:
                return '肥胖'
        if age == 11:
            if num <= 14.2:
                return '中重度消瘦'
            if num < 14.9:
                return '轻度消瘦'
            if num < 20.3:
                return '正常'
            if num < 23.6:
                return '超重'
            if num >= 23.6:
                return '肥胖'
        if age == 12:
            if num <= 14.4:
                return '中重度消瘦'
            if num < 15.4:
                return '轻度消瘦'
            if num < 21.0:
                return '正常'
            if num < 24.7:
                return '超重'
            if num >= 24.7:
                return '肥胖'
        if age == 13:
            if num <= 14.8:
                return '中重度消瘦'
            if num < 15.9:
                return '轻度消瘦'
            if num < 21.9:
                return '正常'
            if num < 25.7:
                return '超重'
            if num >= 25.7:
                return '肥胖'
        if age == 14:
            if num <= 15.3:
                return '中重度消瘦'
            if num < 16.4:
                return '轻度消瘦'
            if num < 22.6:
                return '正常'
            if num < 26.4:
                return '超重'
            if num >= 26.4:
                return '肥胖'
        if age == 15:
            if num <= 15.8:
                return '中重度消瘦'
            if num < 16.9:
                return '轻度消瘦'
            if num < 23.1:
                return '正常'
            if num < 26.9:
                return '超重'
            if num >= 26.9:
                return '肥胖'
        if age == 16:
            if num <= 16.2:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 23.5:
                return '正常'
            if num < 27.4:
                return '超重'
            if num >= 27.4:
                return '肥胖'
        if age == 17:
            if num <= 16.6:
                return '中重度消瘦'
            if num < 17.7:
                return '轻度消瘦'
            if num < 23.8:
                return '正常'
            if num < 27.8:
                return '超重'
            if num >= 27.8:
                return '肥胖'
        if age == 18:
            if num <= 16.8:
                return '中重度消瘦'
            if num < 17.9:
                return '轻度消瘦'
            if num < 24.0:
                return '正常'
            if num < 28.0:
                return '超重'
            if num >= 28.0:
                return '肥胖'
        if age > 18:
            if num <= 16.8:
                return '中重度消瘦'
            if num < 17.9:
                return '轻度消瘦'
            if num >= 17.9:
                return '正常'

    def hight_Bmi_Woman(self, age, num):
        """
        接收传递的值，通过年龄来比对体重指数并返回结果！
        """
        if age == 6:
            if num <= 12.8:
                return '中重度消瘦'
            if num < 13.1:
                return '轻度消瘦'
            if num < 16.5:
                return '正常'
            if num < 18.0:
                return '超重'
            if num >= 18.0:
                return '肥胖'
        if age == 7:
            if num <= 13.0:
                return '中重度消瘦'
            if num < 13.4:
                return '轻度消瘦'
            if num < 17.2:
                return '正常'
            if num < 19.0:
                return '超重'
            if num >= 19.0:
                return '肥胖'
        if age == 8:
            if num <= 13.1:
                return '中重度消瘦'
            if num < 13.6:
                return '轻度消瘦'
            if num < 18.1:
                return '正常'
            if num < 19.9:
                return '超重'
            if num >= 19.9:
                return '肥胖'
        if age == 9:
            if num <= 13.2:
                return '中重度消瘦'
            if num < 13.8:
                return '轻度消瘦'
            if num < 19.0:
                return '正常'
            if num < 21.0:
                return '超重'
            if num >= 21.0:
                return '肥胖'
        if age == 10:
            if num <= 13.3:
                return '中重度消瘦'
            if num < 14.0:
                return '轻度消瘦'
            if num < 20.0:
                return '正常'
            if num < 22.1:
                return '超重'
            if num >= 22.1:
                return '肥胖'
        if age == 11:
            if num <= 13.7:
                return '中重度消瘦'
            if num < 14.3:
                return '轻度消瘦'
            if num < 21.1:
                return '正常'
            if num < 23.3:
                return '超重'
            if num >= 23.3:
                return '肥胖'
        if age == 12:
            if num <= 14.1:
                return '中重度消瘦'
            if num < 14.7:
                return '轻度消瘦'
            if num < 21.9:
                return '正常'
            if num < 24.5:
                return '超重'
            if num >= 24.5:
                return '肥胖'
        if age == 13:
            if num <= 14.6:
                return '中重度消瘦'
            if num < 15.3:
                return '轻度消瘦'
            if num < 22.6:
                return '正常'
            if num < 25.6:
                return '超重'
            if num >= 25.6:
                return '肥胖'
        if age == 14:
            if num <= 15.3:
                return '中重度消瘦'
            if num < 16.0:
                return '轻度消瘦'
            if num < 23:
                return '正常'
            if num < 26.3:
                return '超重'
            if num >= 26.3:
                return '肥胖'
        if age == 15:
            if num <= 16.0:
                return '中重度消瘦'
            if num < 16.6:
                return '轻度消瘦'
            if num < 23.4:
                return '正常'
            if num < 26.9:
                return '超重'
            if num >= 26.9:
                return '肥胖'
        if age == 16:
            if num <= 16.2:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 23.7:
                return '正常'
            if num < 27.4:
                return '超重'
            if num >= 27.4:
                return '肥胖'
        if age == 17:
            if num <= 16.4:
                return '中重度消瘦'
            if num < 17.0:
                return '轻度消瘦'
            if num < 23.9:
                return '正常'
            if num < 27.8:
                return '超重'
            if num >= 27.8:
                return '肥胖'
        if age == 18:
            if num <= 16.7:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num < 24.0:
                return '正常'
            if num < 28.0:
                return '超重'
            if num >= 28.0:
                return '肥胖'
        if age > 18:
            if num <= 16.7:
                return '中重度消瘦'
            if num < 17.3:
                return '轻度消瘦'
            if num >= 17.3:
                return '正常'

    def hight_man(self, age, stature):
        """
        接收传递的值，通过年龄来比对身高并返回结果！
        """
        if age == 6:
            if stature <= 1.063:
                return '生长迟缓'
            else:
                return '正常'
        if age == 7:
            if stature <= 1.113:
                return '生长迟缓'
            else:
                return '正常'
        if age == 8:
            if stature <= 1.154:
                return '生长迟缓'
            else:
                return '正常'
        if age == 9:
            if stature <= 1.206:
                return '生长迟缓'
            else:
                return '正常'
        if age == 10:
            if stature <= 1.252:
                return '生长迟缓'
            else:
                return '正常'
        if age == 11:
            if stature <= 1.291:
                return '生长迟缓'
            else:
                return '正常'
        if age == 12:
            if stature <= 1.331:
                return '生长迟缓'
            else:
                return '正常'
        if age == 13:
            if stature <= 1.369:
                return '生长迟缓'
            else:
                return '正常'
        if age == 14:
            if stature <= 1.419:
                return '生长迟缓'
            else:
                return '正常'
        if age == 15:
            if stature <= 1.496:
                return '生长迟缓'
            else:
                return '正常'
        if age == 16:
            if stature <= 1.551:
                return '生长迟缓'
            else:
                return '正常'
        if age == 17:
            if stature <= 1.568:
                return '生长迟缓'
            else:
                return '正常'
        if age == 18:
            if stature <= 1.571:
                return '生长迟缓'
            else:
                return '正常'
        if age > 18:
            if stature <= 1.572:
                return '生长迟缓'
            else:
                return '正常'

    def hight_woman(self, age, stature):
        """
        接收传递的值，通过年龄来比对身高并返回结果！
        """
        if age == 6:
            if stature <= 1.057:
                return '生长迟缓'
            else:
                return '正常'
        if age == 7:
            if stature <= 1.102:
                return '生长迟缓'
            else:
                return '正常'
        if age == 8:
            if stature <= 1.145:
                return '生长迟缓'
            else:
                return '正常'
        if age == 9:
            if stature <= 1.195:
                return '生长迟缓'
            else:
                return '正常'
        if age == 10:
            if stature <= 1.239:
                return '生长迟缓'
            else:
                return '正常'
        if age == 11:
            if stature <= 1.286:
                return '生长迟缓'
            else:
                return '正常'
        if age == 12:
            if stature <= 1.336:
                return '生长迟缓'
            else:
                return '正常'
        if age == 13:
            if stature <= 1.388:
                return '生长迟缓'
            else:
                return '正常'
        if age == 14:
            if stature <= 1.429:
                return '生长迟缓'
            else:
                return '正常'
        if age == 15:
            if stature <= 1.454:
                return '生长迟缓'
            else:
                return '正常'
        if age == 16:
            if stature <= 1.468:
                return '生长迟缓'
            else:
                return '正常'
        if age == 17:
            if stature <= 1.473:
                return '生长迟缓'
            else:
                return '正常'
        if age == 18:
            if stature <= 1.475:
                return '生长迟缓'
            else:
                return '正常'
        if age > 18:
            if stature <= 1.476:
                return '生长迟缓'
            else:
                return '正常'

    def eyesight1(self, eye):
        """
        国际视力标准读数！
        """
        if eye < 0.25:
            return '重度视力不良'
        if eye < 0.4:
            return '中度视力不良'
        if eye < 0.9:
            return '轻度视力不良'
        if eye >= 0.9:
            return '正常'

    def eyesight3(self, eye):
        """
        中国学生视力标准读数！
        """
        if eye < 4.4:
            return '重度视力不良'
        if eye < 4.6:
            return '中度视力不良'
        if eye < 4.9:
            return '轻度视力不良'
        if eye >= 4.9:
            return '正常'

    def count(self, num):
        """
        统计非空单元格数量
        """
        a = 0
        for i in num[2:]:
            if i.value is None or i.value == '未测':
                pass
            else:
                a += 1
        return a

    def gender(self, num):
        """
        统计男性及女性数量
        """
        man = 0
        women = 0
        for i in num[2:]:
            if i.value == '女':
                women += 1
            if i.value == '男':
                man += 1
        return man, women

    def daojiancount(self, num):
        a = 0
        for i in num[2:]:
            if i.value is None or i.value == 0:
                pass
            else:
                a += 1
        return a

    def wirte_f1(self, fujian):
        """
        以追加的形式生成文件
        """
        if os.path.exists("./report/附件一学生健康体检基本情况汇总.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件一学生健康体检基本情况汇总.xlsx")
            book = workbook['Sheet1']
            book.append([fujian["班级"], fujian["总人数"], fujian["未到人数"], fujian["到检人数"], fujian["检查率"]])
            workbook.save("./report/附件一学生健康体检基本情况汇总.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件一学生健康体检基本情况汇总.xlsx")
            book = workbook['Sheet1']
            book.append([fujian["班级"], fujian["总人数"], fujian["未到人数"], fujian["到检人数"], fujian["检查率"]])
            workbook.save("./report/附件一学生健康体检基本情况汇总.xlsx")
        return

    def wirte_f2(self, fujian):
        if os.path.exists("./report/附件二常见疾病及缺点检出情况一.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件二常见疾病及缺点检出情况一.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["到检人数"],
                fujian["心脏异常"],
                fujian["心脏异常百分比"],
                fujian["肺部异常"],
                fujian["肺部异常百分比"],
                fujian["肝/脾异常"],
                fujian["肝/脾异常百分比"],
                fujian["头部异常"],
                fujian["头部异常百分比"],
                fujian["颈异常"],
                fujian["颈异常百分比"],
                fujian["胸部异常"],
                fujian["胸部异常百分比"],
                fujian["脊柱异常"],
                fujian["脊柱异常百分比"],
                fujian["四肢异常"],
                fujian["四肢异常百分比"],
                fujian["皮肤异常"],
                fujian["皮肤异常百分比"],
                fujian["淋巴结异"],
                fujian["淋巴结异百分比"],
            ])
            workbook.save("./report/附件二常见疾病及缺点检出情况一.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件二常见疾病及缺点检出情况一.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["到检人数"],
                fujian["心脏异常"],
                fujian["心脏异常百分比"],
                fujian["肺部异常"],
                fujian["肺部异常百分比"],
                fujian["肝/脾异常"],
                fujian["肝/脾异常百分比"],
                fujian["头部异常"],
                fujian["头部异常百分比"],
                fujian["颈异常"],
                fujian["颈异常百分比"],
                fujian["胸部异常"],
                fujian["胸部异常百分比"],
                fujian["脊柱异常"],
                fujian["脊柱异常百分比"],
                fujian["四肢异常"],
                fujian["四肢异常百分比"],
                fujian["皮肤异常"],
                fujian["皮肤异常百分比"],
                fujian["淋巴结异"],
                fujian["淋巴结异百分比"],
            ])
            workbook.save("./report/附件二常见疾病及缺点检出情况一.xlsx")
        return

    def wirte_f3(self, fujian):
        if os.path.exists("./report/附件三常见疾病及缺点检出情况二.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件三常见疾病及缺点检出情况二.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["到检人数"],
                fujian["沙眼异常"],
                fujian["沙眼异常百分比"],
                fujian["牙周异常"],
                fujian["牙周异常百分比"],
                fujian["鼻异常"],
                fujian["鼻异常百分比"],
                fujian["喉/扁桃体异常"],
                fujian["喉/扁桃体异常百分比"],
                fujian["轻度视力不良"],
                fujian["轻度视力不良百分比"],
                fujian["中度视力不良"],
                fujian["中度视力不良百分比"],
                fujian["重度视力不良"],
                fujian["重度视力不良百分比"],
                fujian["耳异常"],
                fujian["耳异常百分比"],
                fujian["龋齿异常"],
                fujian["龋齿异常百分比"],
            ])
            workbook.save("./report/附件三常见疾病及缺点检出情况二.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件三常见疾病及缺点检出情况二.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["到检人数"],
                fujian["沙眼异常"],
                fujian["沙眼异常百分比"],
                fujian["牙周异常"],
                fujian["牙周异常百分比"],
                fujian["鼻异常"],
                fujian["鼻异常百分比"],
                fujian["喉/扁桃体异常"],
                fujian["喉/扁桃体异常百分比"],
                fujian["轻度视力不良"],
                fujian["轻度视力不良百分比"],
                fujian["中度视力不良"],
                fujian["中度视力不良百分比"],
                fujian["重度视力不良"],
                fujian["重度视力不良百分比"],
                fujian["耳异常"],
                fujian["耳异常百分比"],
                fujian["龋齿异常"],
                fujian["龋齿异常百分比"],
            ])
            workbook.save("./report/附件三常见疾病及缺点检出情况二.xlsx")
        return

    def wirte_f4(self, fujian):
        if os.path.exists("./report/附件四学生健康体检数据汇总.xlsx"):
            workbook = openpyxl.load_workbook("./report/附件四学生健康体检数据汇总.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["总人数"],
                fujian["到检人数"],
                fujian["未到人数"],
                fujian["男性人数"],
                fujian["女性人数"],
                fujian["男性消瘦"],
                fujian["男性超重"],
                fujian["男性肥胖"],
                fujian["男性生长迟缓"],
                fujian["女性消瘦"],
                fujian["女性超重"],
                fujian["女性肥胖"],
                fujian["女性生长迟缓"],
                fujian["轻度视力不良"],
                fujian["中度视力不良"],
                fujian["重度视力不良"],
                fujian["心脏异常"],
                fujian["肺部异常"],
                fujian["肝/脾异常"],
                fujian["头部异常"],
                fujian["颈异常"],
                fujian["胸部异常"],
                fujian["脊柱异常"],
                fujian["四肢异常"],
                fujian["皮肤异常"],
                fujian["淋巴结异"],
                fujian["耳异常"],
                fujian["鼻异常"],
                fujian["喉/扁桃体异常"],
                fujian["龋齿异常"],
                fujian["牙周异常"]
            ])
            workbook.save("./report/附件四学生健康体检数据汇总.xlsx")
        else:
            workbook = openpyxl.load_workbook("./学校体检模板/附件四学生健康体检数据汇总.xlsx")
            book = workbook['Sheet1']
            book.append([
                fujian["班级"],
                fujian["总人数"],
                fujian["到检人数"],
                fujian["未到人数"],
                fujian["男性人数"],
                fujian["女性人数"],
                fujian["男性消瘦"],
                fujian["男性超重"],
                fujian["男性肥胖"],
                fujian["男性生长迟缓"],
                fujian["女性消瘦"],
                fujian["女性超重"],
                fujian["女性肥胖"],
                fujian["女性生长迟缓"],
                fujian["轻度视力不良"],
                fujian["中度视力不良"],
                fujian["重度视力不良"],
                fujian["心脏异常"],
                fujian["肺部异常"],
                fujian["肝/脾异常"],
                fujian["头部异常"],
                fujian["颈异常"],
                fujian["胸部异常"],
                fujian["脊柱异常"],
                fujian["四肢异常"],
                fujian["皮肤异常"],
                fujian["淋巴结异"],
                fujian["耳异常"],
                fujian["鼻异常"],
                fujian["喉/扁桃体异常"],
                fujian["龋齿异常"],
                fujian["牙周异常"]
            ])
            workbook.save("./report/附件四学生健康体检数据汇总.xlsx")
        return

    def shengzhang(self, num1):
        """
        计算营养吸收人数
        """
        num = []
        # 男性消瘦
        a = 0
        # 男性超重
        b = 0
        # 男性肥胖
        c = 0
        # 女性消瘦
        d = 0
        # 女性超重
        e = 0
        # 女性肥胖
        f = 0
        for x, y in num1:
            if y.value == "轻度消瘦" or y.value == "中重度消瘦":
                if x.value == "男":
                    a += 1
                else:
                    d += 1
            elif y.value == "超重":
                if x.value == "男":
                    b += 1
                else:
                    e += 1
            elif y.value == "肥胖":
                if x.value == "男":
                    c += 1
                else:
                    f += 1
            else:
                pass
        num.append(a)
        num.append(b)
        num.append(c)
        num.append(d)
        num.append(e)
        num.append(f)
        return num

    def shengaojieguo(self, num):
        """
        分别统计男性和女性生长迟缓的人数
        """
        num1 = []
        # 男性生长迟缓
        a = 0
        # 女性生长迟缓
        b = 0
        for x, y in num:
            if y.value == "生长迟缓":
                if x.value == "男":
                    a += 1
                else:
                    b += 1
        num1.append(a)
        num1.append(b)
        return num1

    def shilicount(self, shili):
        """
        计算三种视力程度 分别人数
        """
        # 轻度视力不良计数
        a = 0
        # 中度视力不良计数
        b = 0
        # 重度视力不良计数
        c = 0
        for x in shili:
            if x.value == '轻度视力不良':
                a += 1
            elif x.value == '中度视力不良':
                b += 1
            elif x.value == '重度视力不良':
                c += 1
            else:
                pass
        return [a, b, c]

    def baifenbi(self, a, b):
        """
        计算百分比 如果被除数为0 则直接返回0
        """
        if a == 0 or b == 0:
            return 0
        else:
            return "{:.2%}".format(float(a) / float(b))

    def Sumhe(self, num):
        """
        列纵向求和
        """
        a = 0
        for i in num:
            if i.value == 0 or type(i.value) == str or i.value is None:
                pass
            else:
                a = a + int(i.value)
        return a

    def BMI_result(self, num1, num2):
        num = num1 / num2 ** 2
        return num
